package org.hsieh.tr.explore;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import org.eclipse.core.runtime.Plugin;
import org.eclipse.jface.preference.PreferenceStore;
import org.hsieh.tr.explore.db.ISqlCommand;
import org.hsieh.tr.explore.dbsource.DatabaseInfoManager;
import org.hsieh.tr.explore.dbsource.IDatabaseInfo;
import org.hsieh.tr.explore.exception.ExceptionHandler;
import org.hsieh.tr.explore.ui.SQLCommandManager;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends Plugin {

    private static final String INFOS_XML = "infos.xml";

    public static final String INFOS = "INFOS";

    private static final String SQL_FILE = ".sqls";

    private static final String SQL_KEY = "SQL";

    // The plug-in ID
    public static final String PLUGIN_ID = "org.hsieh.tr.explore";

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
	super.start(context);
	DatabaseInfoManager.INSTANCE.addInfos(restoreDBInfos());
	restoreSQLCommands();
	plugin = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
	plugin = null;
	persistDBInfos(DatabaseInfoManager.INSTANCE.getInfos());
	persistSQLCommand();
	super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static Activator getDefault() {
	return plugin;
    }

    public void persistDBInfos(List<IDatabaseInfo> infos) {
	if (infos == null || infos.isEmpty())
	    return;
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	XMLEncoder encoder = new XMLEncoder(output);
	encoder.writeObject(infos);
	encoder.close();

	PreferenceStore store = new PreferenceStore(INFOS_XML);
	store.setValue(INFOS, output.toString());
	try {
	    store.save();
	} catch (IOException e) {
	}
    }

    @SuppressWarnings("unchecked")
    public List<IDatabaseInfo> restoreDBInfos() {
	PreferenceStore store = new PreferenceStore(INFOS_XML);
	try {
	    store.load();
	    String string = store.getString(INFOS);
	    if (string != null && !string.equals("")) {
		XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(
			string.getBytes()));
		try {

		    List<IDatabaseInfo> infos = (List<IDatabaseInfo>) decoder
			    .readObject();
		    return infos;
		} catch (Exception e) {
		    ExceptionHandler.handle(e);
		}
	    }
	} catch (IOException e1) {
	}
	return null;
    }

    @SuppressWarnings("unchecked")
    public void restoreSQLCommands() {
	PreferenceStore store = new PreferenceStore(SQL_FILE);
	try {
	    store.load();
	    String string = store.getString(SQL_KEY);
	    if (string != null && !string.equals("")) {
		XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(
			string.getBytes()));
		try {

		    List<ISqlCommand> commands = (List<ISqlCommand>) decoder
			    .readObject();
		    SQLCommandManager.getInstance().getCommands().addAll(
			    commands);
		} catch (Exception e) {
		    ExceptionHandler.handle(e);
		}
	    }
	} catch (IOException e1) {
	}
    }

    public void persistSQLCommand() {
	PreferenceStore store = new PreferenceStore(SQL_FILE);
	try {
	    if (!SQLCommandManager.getInstance().getCommands().isEmpty()) {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		XMLEncoder encoder = new XMLEncoder(output);
		encoder.writeObject(SQLCommandManager.getInstance()
			.getCommands());
		encoder.close();
		store.setValue(SQL_KEY, output.toString());
	    }
	    store.save();
	} catch (Exception e1) {
	}
    }

    public String getPluginID() {
	return PLUGIN_ID;
    }

}
